package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.DashPathEffect
import android.graphics.DiscretePathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import com.android.kotlincoroutinemvvmdemo.R

class AbnormalView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null)
    : View(context, attributeSet) {

    private var mWidth = 0
    private var mHeight = 0

    //CornerPathEffect 所有拐角变成圆角
    private val cornerPathEffect by lazy {
        CornerPathEffect(context.resources.getDimension(R.dimen.dp_5))
    }

    //把线条进行随机的偏离，让轮廓变得乱七八糟。
    private val discretePathEffect by lazy {
        DiscretePathEffect(20f, 5f)
    }

    //使用虚线绘制线条
    private val dashPathEffect by lazy {
        DashPathEffect(floatArrayOf(20f, 10f, 5f, 10f), 0f)
    }

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //style = Paint.Style.FILL
            //strokeWidth = context.resources.getDimension(R.dimen.dp_2)
            color = context.resources.getColor(R.color.teal_200)
            pathEffect = dashPathEffect
        }
    }

    private val mFilledPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeWidth = context.resources.getDimension(R.dimen.dp_1)
            color = context.resources.getColor(R.color.color_da8989)
            pathEffect = dashPathEffect
        }
    }

    private val mPath by lazy {
        Path()
    }

    private val mFilledPath by lazy {
        Path()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        mWidth = when (MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when (MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            mPath.reset()
            mPath.moveTo(50f, 40f)
            mPath.lineTo(70f, 100f)
            mPath.lineTo(300f, 100f)
            mPath.lineTo(320f, 300f)
            mPath.lineTo(350f, 180f)
            mPath.lineTo(450f, 180f)
            mPath.lineTo(560f, 285f)
            mPath.lineTo(695f, 145f)
            mPath.lineTo(795f, 445f)
            mPath.lineTo(855f, 325f)
            mPath.lineTo(955f, 395f)
            mPath.lineTo(1025f, 395f)

            mPath.lineTo(1025f, mHeight.toFloat())
            mPath.lineTo(40f, mHeight.toFloat())
            mPath.close()

            drawPath(mPath, mPaint)

            mFilledPath.reset()
            mFilledPath.moveTo(50f, 40f)
            mFilledPath.lineTo(70f, 100f)
            mFilledPath.lineTo(300f, 100f)
            mFilledPath.lineTo(320f, 300f)
            mFilledPath.lineTo(350f, 180f)
            mFilledPath.lineTo(450f, 180f)
            mFilledPath.lineTo(560f, 285f)
            mFilledPath.lineTo(695f, 145f)
            mFilledPath.lineTo(795f, 445f)
            mFilledPath.lineTo(855f, 325f)
            mFilledPath.lineTo(955f, 395f)
            mFilledPath.lineTo(1025f, 395f)
            drawPath(mFilledPath, mFilledPaint)
        }
    }
}